package com.xiaojun.whut.Service.Impl;

import com.alibaba.fastjson.JSON;
import com.xiaojun.whut.Entity.Role;
import com.xiaojun.whut.Entity.User;
import com.xiaojun.whut.Repository.UserRepository;
import com.xiaojun.whut.Service.PcAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

/**
 * @author 鲁佳磊
 * @create 2020-11-02 23:32
 */
@Service
public class PcAdminServiceImpl implements PcAdminService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public int getUserCountByLikeUserName(String username) {
        if (username != null && !"".equals(username)) {
            return userRepository.findByUserNameLike("%" + username + "%").size();
        } else {
            return (int) userRepository.count();
        }
    }


    @Override
    public List<User> getUserListByPageAndLikeUserName(String username, int pageStart, int pageSize) {
        if (username != null && !"".equals(username)) {
            return userRepository.getUserListByPageAndLikeUserName("%" + username + "%", pageStart, pageSize);
        } else {
            return userRepository.getUserListByPage(pageStart, pageSize);
        }
    }




    @Override
    public String getAllUserList(String query, int pageNum, int pageSize) throws Exception {

        int userCounts = this.getUserCountByLikeUserName(query);
        int pageStart = pageSize * (pageNum - 1);
        List<User> allUser = this.getUserListByPageAndLikeUserName(query, pageStart, pageSize);
        HashMap<String, Object> res = new HashMap<>();
        res.put("number", userCounts);
        res.put("data", allUser);
        return JSON.toJSONString(res);

    }


    @Override
    public String addUserService(String username, String password, String email, String phoneNumber, String collage, String sex) throws Exception {

            User user = new User();
            user.setPhoneNumber(phoneNumber);
            user.setPassword(password);
            user.setEmail(email);
            user.setCollage(collage);
            user.setUserName(username);
            user.setSex(User.Sex.valueOf(sex));

            user.setEnable(true);           //初始化的时候就是这个样子,设置这个可用还有这个没有上锁，然后就可以进行之后的操作
            user.setLocked(false);

            HashSet<Role> roles = new HashSet<>();          //添加这个角色的信息
            Role role = new Role();
            role.setName("user");           //最开始的地方是设置的是普通的用户
            role.setNameZH("普通用户");
            roles.add(role);
            user.setRoles(roles);

            userRepository.save(user);      //保存了一个用户
            System.out.println("增加这个一个用户！！！");
            return "200";
    }

    @Override
    public String updateUserState(String phoneNumber, boolean enable) throws Exception {
        User byPhoneNumber = userRepository.findByPhoneNumber(phoneNumber);
        byPhoneNumber.setEnable(enable);
        userRepository.save(byPhoneNumber);
        return "200";
    }

    @Override
    public String deleteUserById(String phoneNumber) throws Exception {
        userRepository.deleteByPhoneNumber(phoneNumber);
        return "200";
    }

    @Override
    public String editUserInfo(String username, String password, String email, String phoneNumber, String collage, String sex) throws Exception {
        User byUserName = userRepository.findByUserName(username);
        byUserName.setSex(User.Sex.valueOf(sex));
        byUserName.setCollage(collage);
        byUserName.setEmail(email);
        userRepository.save(byUserName);
        return "200";
    }

    @Override
    public String updateAuthorites(String phoneNumber, String authorities) throws Exception {
        User byPhoneNumber = userRepository.findByPhoneNumber(phoneNumber);
        String[] split = authorities.split(",");
        HashSet<Role> roles = new HashSet<>();          //添加这个角色的信息
        for (int i = 0; i < split.length; i++) {
            Role role = new Role();
            role.setName(split[i]);
            switch (split[i]){
                case "admin":
                    role.setNameZH("系统管理员");
                    break;
                case "user":
                    role.setNameZH("普通用户");
                    break;
            }
            roles.add(role);
        }
        byPhoneNumber.setRoles(roles);
        userRepository.save(byPhoneNumber);     //保存信息的东西在这个地方
        return "200";
    }
}
